package gui2;

import java.awt.BorderLayout;
import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JButton;
import java.awt.GridLayout;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;

import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JSpinner;
import javax.swing.JTextField;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.SpinnerDateModel;
import javax.swing.border.TitledBorder;

import schedule.Appointment;
import schedule.AppointmentList;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

public class EditWindow extends JFrame {

	private JPanel contentPane;
	private JTextField tfTitle;
//	private JTextField tfStart;
//	private JTextField tfEnd;
	private JTextArea textArea;
	
	private SpinnerDateModel startDate = new SpinnerDateModel();
	private JSpinner spStart;
	private SpinnerDateModel endDate = new SpinnerDateModel();
	private JSpinner spEnd;
	
	private AppointmentList list;
	private Appointment app;
	
	/**
	 * Launch the application.
	 */
//	public static void main(String[] args) {
//		EventQueue.invokeLater(new Runnable() {
//			public void run() {
//				try {
//					editWindow frame = new editWindow();
//					frame.setVisible(true);
//				} catch (Exception e) {
//					e.printStackTrace();
//				}
//			}
//		});
//	}

	/**
	 * Create the frame.
	 */
	public EditWindow(Appointment app, AppointmentList list) {
		this.list = list;
		this.app = app;
		
		setTitle("Edit");
		setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
		setBounds(100, 100, 290, 440);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		contentPane.setLayout(new BorderLayout(0, 0));
		setContentPane(contentPane);
		
		JPanel panel = new JPanel();
		contentPane.add(panel, BorderLayout.NORTH);
		panel.setLayout(new GridLayout(3, 2, 0, 0));
		
		JLabel lblTitle = new JLabel("Title:");
		panel.add(lblTitle);
		
		tfTitle = new JTextField();
		panel.add(tfTitle);
		tfTitle.setColumns(10);
		tfTitle.setText(app.getTitle());
		
		JLabel lblStart = new JLabel("Start:");
		panel.add(lblStart);
				
//		tfStart = new JTextField();
//		panel.add(tfStart);
//		tfStart.setColumns(10);
//		tfStart.setText(getFormatedDateString(app.getDate()));
		
		spStart = new JSpinner(startDate);
		spStart.setEditor(new JSpinner.DateEditor(spStart, "dd.MM.yyyy HH:mm"));
		spStart.setValue(app.getDate());
		panel.add(spStart);
		
		JLabel lblEnd = new JLabel("End:");
		panel.add(lblEnd);
		
//		tfEnd = new JTextField();
//		panel.add(tfEnd);
//		tfEnd.setColumns(10);
//		tfEnd.setText(getFormatedDateString(app.getEnd()));
		
		spEnd = new JSpinner(endDate);
		spEnd.setEditor(new JSpinner.DateEditor(spEnd, "dd.MM.yyyy HH:mm"));
		spEnd.setValue(app.getEnd());
		panel.add(spEnd);
		
		JPanel panel_1 = new JPanel();
		contentPane.add(panel_1, BorderLayout.SOUTH);
		
		JButton btnEdit = new JButton("Edit");
		btnEdit.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				do_btnEdit_actionPerformed(e);
			}
		});
		panel_1.add(btnEdit);
		
		JScrollPane scrollPane = new JScrollPane();
		contentPane.add(scrollPane, BorderLayout.CENTER);
		
		textArea = new JTextArea();
		textArea.setBorder(new TitledBorder(null, "Description:", TitledBorder.LEADING, TitledBorder.TOP, null, null));
		scrollPane.setViewportView(textArea);
		textArea.setText(app.getDescription());
		
		this.setVisible(true);
	}
	
	public String getFormatedDateString(Date date){
		Calendar myCal = new GregorianCalendar();
		myCal.setTime(date);
		String year, month, day, hour, minute;
		String formated = ""; //YYYYMMDD hhmm
		
		if(myCal.get(Calendar.YEAR) >= 0){
			if(myCal.get(Calendar.YEAR) < 10)
				year = "000"+ myCal.get(Calendar.YEAR);
				else
					if (myCal.get(Calendar.YEAR) < 100)
						year = "00"+ myCal.get(Calendar.YEAR);
					else
						if(myCal.get(Calendar.YEAR) < 1000 )
							year = "0" + myCal.get(Calendar.YEAR);
						else
							year = ""+myCal.get(Calendar.YEAR); //years bigger than 9999 won't be handled because they cannot be entered in this program
		} else
			year = "0000"; //not important: negative dates cannot be entered in this program
		
		if(myCal.get(Calendar.MONTH)+1 < 10)
			month = "0"+ (myCal.get(Calendar.MONTH)+1);
		else
			month = ""+(myCal.get(Calendar.MONTH)+1);
		
		if(myCal.get(Calendar.DAY_OF_MONTH)<10)
			day = "0"+myCal.get(Calendar.DAY_OF_MONTH);
		else
			day = ""+myCal.get(Calendar.DAY_OF_MONTH);
		
		if(myCal.get(Calendar.HOUR_OF_DAY)<10)
			hour = "0"+myCal.get(Calendar.HOUR_OF_DAY);
		else
			hour = ""+myCal.get(Calendar.HOUR_OF_DAY);
		
		if(myCal.get(Calendar.MINUTE)<10)
			minute = "0"+myCal.get(Calendar.MINUTE);
		else
			minute = ""+ myCal.get(Calendar.MINUTE);
		
		formated = year+month+day+" "+hour+minute;
		return formated;
	}

	protected void do_btnEdit_actionPerformed(ActionEvent e) {
//		SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd hhmm");
		boolean added = false;	//used adding a new and removing an old appointment to make sure all possible exceptions are checked before changing anything
		Appointment edited;
		try{	
			if(!tfTitle.getText().equals("")){
//				Date convertedDate = (Date) sdf.parse(tfStart.getText());
//				Date convertedEndDate = (Date) sdf.parse(tfEnd.getText());
				edited = new Appointment(tfTitle.getText(),startDate.getDate(), endDate.getDate(), textArea.getText());
				boolean worked = list.add(edited, app);
				edited.setId(app.getId());
				list.bubbleSort();
				added = true;
				if(!worked){
					throw new Exception("please check for overlaps with existing appointments");
				}
			} else{
				throw new Exception("Title empty");
			}
		}catch(ParseException pe){
			JOptionPane.showMessageDialog(this, pe.getMessage());
		}catch(Exception ex){
			JOptionPane.showMessageDialog(this, ex.getMessage());
		}
		if(added){
			list.remove(app);
			list.bubbleSort();
		}
	}
}
